package uk.org.landeg.euler.problems;

import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import uk.org.landeg.euler.problems.framework.AbstractProblem;
import uk.org.landeg.euler.problems.framework.ProblemLauncher;
import uk.org.landeg.euler.problems.lib.NumbersLib;

public class Problem074 extends AbstractProblem {
	public static void main(String[] args) {
		ProblemLauncher launcher = new ProblemLauncher(new Problem074());
		launcher.solve();
	}

	@Override
	public String getDescription() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer getProblemNumber() {
		// TODO Auto-generated method stub
		return 74;
	}

	int factorials[] = new int [10];

	@Override
	public Object solve() {
		for (int i = 0 ; i < factorials.length ;i++){
			factorials[i] = (int)NumbersLib.factorial(i);
		}
		int test = 52;
		Map<Integer,Integer>allPrevoius = new HashMap<Integer,Integer>();
		Vector<Integer> previous ;
		int itterations;
		int chain60s = 0;
		boolean seenBefore;
		
		for (int idx =1 ; idx < 1000000 ; idx++) {
			previous= new Vector<Integer>();
			test=idx;
			itterations = 0;
			do {
				previous.add(test);
				if (allPrevoius.containsKey(test)) {
					test = allPrevoius.get(test);
				} else {
					test = itterate(test);
				}
				itterations++;
			} while (!previous.contains(test));
			if (previous.size() == 60) {
				chain60s++;
			}
		}
		notes.add("Runs timed on crinkly old laptop");
		notes.add("Original runtime 31993ms, no optimization and some verbose.");
		notes.add("Removed verbose - runtime : 24009ms");
		return chain60s;
	}
	
	private int itterate (int n) {
		int sum = 0;
		int digits[] = NumbersLib.getDigits(n);
		for (int d : digits) {
			sum += factorials[d];
		}
		return sum;
	}

}
